﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using MxWeiXinPF.DBUtility;
using System.Collections.Generic;
using MxWeiXinPF.Common;//Please add references
namespace MxWeiXinPF.DAL
{
	/// <summary>
	/// 数据访问类:wx_shop_albums
	/// </summary>
    public partial class wx_shop_albums
    {
        public int Add(MxWeiXinPF.Model.wx_shop_albums model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into wx_shop_albums(");
            builder.Append("productId,thumb_path,original_path,remark,add_time,wid,isdefault)");
            builder.Append(" values (");
            builder.Append("@productId,@thumb_path,@original_path,@remark,@add_time,@wid,@isdefault)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@productId", SqlDbType.Int, 4), new SqlParameter("@thumb_path", SqlDbType.VarChar, 800), new SqlParameter("@original_path", SqlDbType.VarChar, 800), new SqlParameter("@remark", SqlDbType.VarChar, 500), new SqlParameter("@add_time", SqlDbType.DateTime), new SqlParameter("@wid", SqlDbType.Int, 4), new SqlParameter("@isdefault", SqlDbType.VarChar, 10) };
            cmdParms[0].Value = model.productId;
            cmdParms[1].Value = model.thumb_path;
            cmdParms[2].Value = model.original_path;
            cmdParms[3].Value = model.remark;
            cmdParms[4].Value = model.add_time;
            cmdParms[5].Value = model.wid;
            cmdParms[6].Value = model.isdefault;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public MxWeiXinPF.Model.wx_shop_albums DataRowToModel(DataRow row)
        {
            MxWeiXinPF.Model.wx_shop_albums _albums = new MxWeiXinPF.Model.wx_shop_albums();
            if (row != null)
            {
                if ((row["id"] != null) && (row["id"].ToString() != ""))
                {
                    _albums.id = int.Parse(row["id"].ToString());
                }
                if ((row["productId"] != null) && (row["productId"].ToString() != ""))
                {
                    _albums.productId = int.Parse(row["productId"].ToString());
                }
                if (row["thumb_path"] != null)
                {
                    _albums.thumb_path = row["thumb_path"].ToString();
                }
                if (row["original_path"] != null)
                {
                    _albums.original_path = row["original_path"].ToString();
                }
                if (row["remark"] != null)
                {
                    _albums.remark = row["remark"].ToString();
                }
                if ((row["add_time"] != null) && (row["add_time"].ToString() != ""))
                {
                    _albums.add_time = new DateTime?(DateTime.Parse(row["add_time"].ToString()));
                }
                if ((row["wid"] != null) && (row["wid"].ToString() != ""))
                {
                    _albums.wid = new int?(int.Parse(row["wid"].ToString()));
                }
                if (row["isdefault"] != null)
                {
                    _albums.isdefault = row["isdefault"].ToString();
                }
            }
            return _albums;
        }

        public bool Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from wx_shop_albums ");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string idlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from wx_shop_albums ");
            builder.Append(" where id in (" + idlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public void DeleteList(SqlConnection conn, SqlTransaction trans, List<MxWeiXinPF.Model.wx_shop_albums> models, int productId)
        {
            StringBuilder builder = new StringBuilder();
            if (models != null)
            {
                foreach (MxWeiXinPF.Model.wx_shop_albums _albums in models)
                {
                    if (_albums.id > 0)
                    {
                        builder.Append(_albums.id + ",");
                    }
                }
            }
            string str = Utils.DelLastChar(builder.ToString(), ",");
            StringBuilder builder2 = new StringBuilder();
            builder2.Append("select id,thumb_path,original_path from wx_shop_albums where productId=" + productId);
            if (!string.IsNullOrEmpty(str))
            {
                builder2.Append(" and id not in(" + str + ")");
            }
            DataSet set = DbHelperSQL.Query(conn, trans, builder2.ToString());
            foreach (DataRow row in set.Tables[0].Rows)
            {
                int num = DbHelperSQL.ExecuteSql(conn, trans, "delete from  wx_shop_albums  where id=" + row["id"].ToString());
            }
        }

        public bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from wx_shop_albums");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select id,productId,thumb_path,original_path,remark,add_time,wid,isdefault ");
            builder.Append(" FROM wx_shop_albums ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" id,productId,thumb_path,original_path,remark,add_time,wid,isdefault ");
            builder.Append(" FROM wx_shop_albums ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.id desc");
            }
            builder.Append(")AS Row, T.*  from wx_shop_albums T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "wx_shop_albums");
        }

        public MxWeiXinPF.Model.wx_shop_albums GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 id,productId,thumb_path,original_path,remark,add_time,wid,isdefault from wx_shop_albums ");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@id", SqlDbType.Int, 4) };
            cmdParms[0].Value = id;
            MxWeiXinPF.Model.wx_shop_albums _albums = new MxWeiXinPF.Model.wx_shop_albums();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM wx_shop_albums ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(MxWeiXinPF.Model.wx_shop_albums model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update wx_shop_albums set ");
            builder.Append("productId=@productId,");
            builder.Append("thumb_path=@thumb_path,");
            builder.Append("original_path=@original_path,");
            builder.Append("remark=@remark,");
            builder.Append("add_time=@add_time,");
            builder.Append("wid=@wid,");
            builder.Append("isdefault=@isdefault");
            builder.Append(" where id=@id");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@productId", SqlDbType.Int, 4), new SqlParameter("@thumb_path", SqlDbType.VarChar, 800), new SqlParameter("@original_path", SqlDbType.VarChar, 800), new SqlParameter("@remark", SqlDbType.VarChar, 500), new SqlParameter("@add_time", SqlDbType.DateTime), new SqlParameter("@wid", SqlDbType.Int, 4), new SqlParameter("@id", SqlDbType.Int, 4), new SqlParameter("@isdefault", SqlDbType.VarChar, 10) };
            cmdParms[0].Value = model.productId;
            cmdParms[1].Value = model.thumb_path;
            cmdParms[2].Value = model.original_path;
            cmdParms[3].Value = model.remark;
            cmdParms[4].Value = model.add_time;
            cmdParms[5].Value = model.wid;
            cmdParms[6].Value = model.id;
            cmdParms[7].Value = model.isdefault;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

